﻿/*******************************************************************************
* Copyright (C) JuCheap
* 
* Author: JuCheap
* Create Date: 2019/12/26 11:21:05
* Description: <Description>
* 
* Revision History:
* Date         Author               Description
*
*********************************************************************************/

using JuCheap.Core.Infrastructure;
using JuCheap.Core.Infrastructure.Extentions;
using JuCheap.Core.IRepository;
using JuCheap.Core.IService;
using JuCheap.Core.Model;
using System.Linq;
using System.Threading.Tasks;

namespace JuCheap.Core.Service
{
    /// <summary>
    /// 系统日志查询服务
    /// </summary>
    public class SysLogQueryService : ISysLogQueryService
    {
        private readonly ISysLoginLogRepository _sysLoginLogRepository;
        private readonly ISysVisitLogRepository _sysVisitLogRepository;

        /// <summary>
        /// ctor
        /// </summary>
        public SysLogQueryService(ISysLoginLogRepository sysLoginLogRepository, ISysVisitLogRepository sysVisitLogRepository)
        {
            _sysLoginLogRepository = sysLoginLogRepository;
            _sysVisitLogRepository = sysVisitLogRepository;
        }

        /// <summary>
        /// 分页查询登陆日志
        /// </summary>
        /// <param name="filter">分页条件</param>
        /// <returns></returns>
        public async Task<PagedResult<SysLoginLogDto>> QueryLoginLogsPagesAsync(SysLoginLogFilter filter)
        {
            var startDate = filter.GetLoginStartDate();
            var endDate = filter.GetLoginEndDate();
            var query = _sysLoginLogRepository.Queryable()
                .WhereIf(startDate.HasValue, x => x.CreateTime > startDate.Value)
                .WhereIf(endDate.HasValue, x => x.CreateTime < endDate.Value)
                .Select(x => new SysLoginLogDto
                {
                    Id = x.Id,
                    Result = x.Result,
                    CreateTime = x.CreateTime
                });

            return await query.QueryPagesAsync(filter);
        }

        /// <summary>
        /// 分页查询访问日志
        /// </summary>
        /// <param name="filter">分页条件</param>
        /// <returns></returns>
        public async Task<PagedResult<SysVisitLogDto>> QueryVisitLogsPagesAsync(SysVisitLogFilter filter)
        {
            var startDate = filter.GetStartDate();
            var endDate = filter.GetEndDate();
            var query = _sysVisitLogRepository.Queryable()
                .WhereIf(startDate.HasValue, x => x.CreateTime > startDate.Value)
                .WhereIf(endDate.HasValue, x => x.CreateTime < endDate.Value)
                .Select(x => new SysVisitLogDto
                {
                    Id = x.Id,
                    IP = x.IP,
                    Url = x.Url,
                    UserName = x.UserName,
                    LoginAccount = x.LoginAccount,
                    CreateTime = x.CreateTime
                });

            return await query.QueryPagesAsync(filter);
        }
    }
}
